package org.naxx.security.model.impl;

import org.naxx.security.common.Decision;
import org.naxx.security.model.Condition;
import org.naxx.security.model.Rule;
import org.naxx.security.model.Target;

/**
 * Two rules are equal if they have same id.
 */
public class RuleImpl implements Rule {
	
	private Condition condition;
	private String description;
	private Decision effect;
	private String id;
	private Target target;
	
	public RuleImpl() {
	}

	public RuleImpl(String id, Decision effect, Condition condition, 
			Target target, String description) {
		this.assertEffect(effect);
		this.condition = condition;
		this.description = description;
		this.effect = effect;
		this.id = id;
		this.target = target;
	}
	
	public Condition getCondition() {
		return this.condition;
	}
	public String getDescription() {
		return this.description;
	}
	public Decision getEffect() {
		return this.effect;
	}
	public String getId() {
		return this.id;
	}
	public Target getTarget() {
		return this.target;
	}
	public void setCondition(Condition condition) {
		this.condition = condition;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public void setEffect(Decision effect) {
		this.assertEffect(effect);
		this.effect = effect;
	}
	public void setId(String id) {
		this.id = id;
	}
	public void setTarget(Target target) {
		this.target = target;
	}

	public boolean equals(Object obj) {
		if (this == obj) {
		    return true;
		}
		if (obj instanceof Rule) {
			Rule rhs = (Rule) obj;
			return this.id.equals(rhs.getId());
		}
		return false;
	}

	public int hashCode() {
		return this.id.hashCode();
	}
	
	private void assertEffect(Decision effect) {
		if(Decision.NOT_APPLICABLE.equals(effect) 
				|| Decision.INDETERMINATE.equals(effect)) {
			
			throw new IllegalArgumentException("Rule effect can not be" +
					" '" + effect.name() + "'");
		}
	}
}
